<?php
/**
 *
 *  Newer, spiffier email class...replaces mailAPI.php
 *
 *  Emailing class
 *
 *  @author     David Lasley, dave -at- dlasley -dot- net
 *  @package    pyhp-imap
 *  @version    $Id: class.mail_api.php 2 2012-12-12 19:35:18Z dave@dlasley.net $
 *
 *  @required   PEAR Mail/mime
 *  @required   http://code.google.com/p/pass-extract/
 * 
 **/
define( '__DEBUG__'     ,   (gethostname() == 'prod_server_hostname.prod') ? false : true);/*   Debug?  */
define( '__SHARED_DATA__'  ,   dirname(__FILE__)   .'/../');/
define( '__EMAIL_INI__' ,   __SHARED_DATA__.'emailData.ini');
class mail_api{
    /*
     *  Construct
     */
    public function __construct($mail_list=false){
        $this->includes = get_include_path();
        set_include_path('/usr/share/pear/');
        require_once "Mail.php";
        require_once "Mail/mime.php";
        require_once(__SHARED_DATA__.'pass_extract.php'); #<http://code.google.com/p/pass-extract/
        //  Exchange Server Info
        $pass = new pass_extraction('AD','TeamReporting'); #<http://code.google.com/p/pass-extract/
        $mail_settings = new stdClass;
        $mail_settings->host    =   'exchange.company.com';
        $mail_settings->port    =   '465';
        $mail_settings->username=   'COMPANY\\'.$pass->username;
        $mail_settings->pass    =   $pass->current_pass;
        $this->mail_settings    =   $mail_settings;
        if($mail_list)
            $this->mailing_list = $mail_list;
        $this->email_css = __SHARED_DATA__.'email_styling.css';
        return $this;
    }
    /*
     *  Set include path back
     */
    public function __destruct(){
        set_include_path($this->includes);
    }
    /*
     *  Sends formatted email with attachments
     *
     *  @param  array   $mailTo             Mail To
     *  @param  string  $subject            eMail Subject
     *  @param  string  $body               eMail Message
     *  @param  array   $ccTo               CC to
     *  @param  array   $headers            Non-default Headers
     *  @param  array   $attachments        Attachments (non-inline)
     *  @param  array   $inlineAttachments  Inline attachments (key needs to be cid, which should correspond to a cid in an IMG tag in $body)
     *  @param  array   $bccTo              BCC To
     *  @param  string  $originalMsg        Original Email (to put at bottom of this one)
     *
     *  @return bool    true/false success/fail
     */
    public function send_email($mailTo,$subject,$body,$ccTo,$headers=array(),
                                 $attachments=array(),$inlineAttachments=array(),$bccTo=false,
                                 $originalMsg=''){
        $subject = (__DEBUG__) ? $subject . ' (DEV_BOX)' : $subject;
        if($this->mailing_list){
            foreach($this->mailing_list['to'] as $single){
                $mailTo[] = $single;
            }
            foreach($this->mailing_list['cc'] as $single){
                $ccTo[] = $single;
            }
            foreach($this->mailing_list['bcc'] as $single){
                $bccTo[] = $single;
            }
        }
        $mailTo =   implode(',',$mailTo);
        $toString = $mailTo;
        if($ccTo){
            $ccTo   =   implode(',',$ccTo);
            $toString .=','.$ccTo;
        }
        if($bccTo){
            $bccTo  =   implode(',',$bccTo);
            $toString .=','.$bccTo;
        }
        $default_headers = array(
            'FROM'      =>  'DATAIT <TeamReporting@company.com>',
            'Reply-To'  =>  'Team Team <TeamTeam@company.com>',
            'Cc'        =>  $ccTo,
            'Subject'   =>  $subject,
            'To'        =>  $mailTo,
            'Bcc'       =>  $bccTo,
        );
        foreach($default_headers as $key=>$header){
            $headers[$key] = $header;
        }
        foreach($headers as $key=>$header){
            $headerStr .= sprintf('%s: %s'."\r\n", $key,$header);
        }
        $crlf   =   "\n"; 
        $mime   =   new Mail_mime($crlf);
        $ini    =   parse_ini_file(__EMAIL_INI__);
        $ini['mailBottom'] = str_replace('$internal',$internal,$ini['mailBottom']);
        $ini['mailBottom'] = str_replace('$originalMsg',$originalMsg,$ini['mailBottom']);
        $mailTop    =   $ini['mailTop'];
        $mailBottom =   $ini['mailBottom'];
        $id     =   $ini['logoID'];
        $logo   =   $ini['logoPath'];
        $mime->setTXTBody($ini['txtBody']);
        $mime->addHTMLImage($logo, mime_content_type($logo), 'company_logo.png', true, $id);
        foreach($inlineAttachments as $cid=>$path){
            $mime->addHTMLImage($path, mime_content_type($path), $cid, true, $cid);
        }
        foreach($attachments as $path){
            $mime->addAttachment($path, mime_content_type($path), basename($path));
        }
        $mime->setHTMLBody($mailTop.$body.$mailBottom);
        $body = $mime->get();
        $headers = $mime->headers($headers);
        $smtp = Mail::factory('smtp',
                              array(
                                    'host'      =>  $this->mail_settings->host,
                                    'auth'      =>  true,
                                    'username'  =>  $this->mail_settings->username,
                                    'password'  =>  $this->mail_settings->pass
                            )   );
        $mail = $smtp->send($toString,$headers,$body);
        if(PEAR::isError($mail)){
            $this->_log[] = $mail->getMessage();
            return false;
        }else{
            return true;
        }
    }
}
?>